/**
 * @author Juko, Apocrypha
 */
package ganga.custodian.settings;

import ganga.custodian.CustodianApplication;
import ganga.custodian.R;
import ganga.custodian.ui.ListViewAdapter;
import ganga.custodian.ui.ListViewItem;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;


public class MainSettingsActivity extends Activity {

	private CustodianApplication application = CustodianApplication.getInstance();

	enum Itmes {
		PHYSIOLOGY, MODEL, LOCATION, ACCELEROMETER, CARDIOMONITOR, THEMES, CALIBRATION, USERS
	}

	private static final int request_SelectCardioMonitor_Code = 301;

	Class[] activities = new Class[8];

	class Controller {

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setCurrentTheme();
		try {
			setContentView(R.layout.sittingsmain);
			buildListView(R.id.ListView01);
		} catch (Exception e) {
			Log.e("initializeListView", e.getMessage());
		}
		activities[4] = ganga.custodian.settings.CardioMonitorParameters.class;
		//selectCardioMonitor = (Spinner)findViewById(R.id.mainSettingsCardioMonitorSpinner);
		//selectCardioMonitor.setVisibility(View.INVISIBLE);
	}
	Spinner selectCardioMonitor;
	@Override
	protected void onResume() {
		super.onResume();
		//setCurrentTheme();
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case request_SelectCardioMonitor_Code:
			if (resultCode != Activity.RESULT_OK) 
				break;
			 
			String selected = data.getExtras().getString(SelectCardioMonitor.CARDIO_MONITOR_NAME);
			application.cardioMonitor = selected;
			Log.i("onActivityResult", Integer.toString(resultCode)+ " "+ selected);
			
			break;

		default:
			break;
		}
		//super.onActivityResult(requestCode, resultCode, data);
	}
	void startSelectCardioMonitorActivity(){
		Intent intent = new Intent(this, ganga.custodian.settings.SelectCardioMonitor.class);
		startActivityForResult(intent, request_SelectCardioMonitor_Code);
	}
	private void startActivityByID(int position) {
		//CARDIOMONITOR
		if (position==4){ 
			startActivity(new Intent(this, activities[4]));
		
		}
		switch (position) {
		case (0): {
			Intent i1 = new Intent(
					ganga.custodian.settings.MainSettingsActivity.this,
					ganga.custodian.sandbox.PatientCard.class);
			startActivity(i1);
			return;
		}
		case (2): {
			Intent i1 = new Intent(
					ganga.custodian.settings.MainSettingsActivity.this,
					ganga.custodian.settings.GPSParameterActivity.class);
			startActivity(i1);
			return;
		}
		case (5): {
			Intent i1 = new Intent(
					ganga.custodian.settings.MainSettingsActivity.this,
					ganga.custodian.settings.ThemsActivity.class);
			startActivity(i1);
			return;
		}
		case (6): {
			Intent i1 = new Intent(
					ganga.custodian.settings.MainSettingsActivity.this,
					ganga.custodian.settings.CalibrationActivity.class);
			startActivity(i1);
			return;
		}
		case (7): {
			Intent i1 = new Intent(
					ganga.custodian.settings.MainSettingsActivity.this,
					ganga.custodian.security.IdentificationActivity.class);
			startActivity(i1);
			return;
		}

		default: {
			return;
		}

		}
	}

	private void buildListView(int listVewId) {
		ListView listView = (ListView) findViewById(listVewId);

		final List<ListViewItem> itemsList = new ArrayList<ListViewItem>();
		itemsList.add(new ListViewItem("Физиология", "Настройка физиологических параметров человека", R.drawable.mario));
		itemsList.add(new ListViewItem("Модель", "Настройка модели", R.drawable.icon8));
		itemsList.add(new ListViewItem("Местоположение", "Параметры датчиков GPS", R.drawable.radar));
		itemsList.add(new ListViewItem("Акселерометр", "Настройка акселерометра", R.drawable.accelerometer));
		itemsList.add(new ListViewItem("Кардиомонитор", "Настройка кардиомонитора", R.drawable.heart));
		itemsList.add(new ListViewItem("Темы", "Настройка тем", R.drawable.theme1));
		itemsList.add(new ListViewItem("Калибровка", "Калибровка датчиков", R.drawable.calibro));
		itemsList.add(new ListViewItem("Выбор пользователя", "Идентификация", R.drawable.keyicon));

		//Проинициализировать отображаемый список
		ListViewAdapter adapter = new ListViewAdapter(this, itemsList);
		listView.setAdapter(adapter);

		//Установить обработчики событий
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long index) {
				startActivityByID(position);
			}
		});

		listView.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int position, long index) {
				if (position == 3) {
					Intent i1 = new Intent(
							ganga.custodian.settings.MainSettingsActivity.this,
							ganga.custodian.settings.AccelerometrPropertyActivity.class);
					startActivity(i1);
				};
				if (position == 4){
					//selectCardioMonitor.setVisibility(View.VISIBLE);
					startSelectCardioMonitorActivity();
					return true;
				}
				//set true if work is done
				return false;
			}
		});

		listView.setClickable(true);
	}

	private void setCurrentTheme() {
		ListView listView = (ListView) findViewById(R.id.ListView01);
		listView.setCacheColorHint(CustodianApplication.getInstance().selectTheme.backGround);
		listView.setBackgroundColor(CustodianApplication.getInstance().selectTheme.backGround);
	}
}